package com.cybertooth.dbconvenience.query.columns;

import com.cybertooth.dbconvenience.query.core.SqlTransformer;
import com.google.inject.Inject;

// TODO: promote to interface
// TODO: need to think about being able to pass the AS whereClause...

/**
 * <p></p>
 *
 * @author dnelson
 */
public class ColumnWithAs implements SqlTransformer {

	// -----------------------------------------------------------------------Fields

	protected final ColumnNameTransformer asClauseGenerator;
	protected Column column;

	// -----------------------------------------------------------------Constructors

	public static ColumnWithAs column(String prefix, String name) {
		// TODO: use injection
		ColumnWithAs instance = new ColumnWithAs(new UnderscoreToCamelCase());
		instance.column = new Column(prefix, name);
		return instance;
	}

	public static ColumnWithAs column(String name) {
		// TODO: use injection
		ColumnWithAs instance = new ColumnWithAs(new UnderscoreToCamelCase());
		instance.column = new Column(name);
		return instance;
	}

	/**
	 * Even though this is a public constructor, you MUST use the static sugar calls as they will be using injection to
	 * provide the {@link com.cybertooth.dbconvenience.query.columns.ColumnNameTransformer}.  <em>There is no public access
	 * to the the {@link #column} field which renders this constructor useless to most applications (testing
	 * excluded).</em>
	 *
	 * @param asClauseGenerator the injected {@link com.cybertooth.dbconvenience.query.columns.ColumnNameTransformer}
	 *                          instance.
	 * @see #column(String)
	 * @see #column(String, String)
	 */
	@Inject
	public ColumnWithAs(ColumnNameTransformer asClauseGenerator) {
		this.asClauseGenerator = asClauseGenerator;
	}

	// ---------------------------------------------------------------Public Methods

	/**
	 * {@inheritDoc}
	 */
	@Override
	public String toSql() {
		return new StringBuilder()
				.append(this.column.toSql()).append("AS ").append(asClauseGenerator.transform(this.column.getName()))
				.toString();
	}
}
